package com.yswy.backstage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.service.SsoService;
import com.yswy.backstage.service.TgMediaAccountService;
import com.yswy.backstage.service.UserProjectService;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.Domain;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.entity.Softness;
import com.yswy.domain.entity.UserProject;
import com.yswy.domain.entity.tf.TgMediaAccount;
import com.yswy.domain.entity.user.User;
import com.yswy.domain.framework.context.CurrentUserContext;
import com.yswy.domain.mapper.UserProjectMapper;
import com.yswy.domain.mapper.tf.TgMediaAccountMapper;
import com.yswy.domain.vo.dto.BaseFilter;
import com.yswy.domain.vo.gld.addOrUpate.TgMediaAccountAddOrUpdate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

import static com.yswy.backstage.utils.CrudUtils.selectCount;
import static com.yswy.common.constants.WarnMsgConstants.NULL_MSG;

@Slf4j
@Service
public class TgMediaAccountServiceImpl extends ServiceImpl<TgMediaAccountMapper, TgMediaAccount>
        implements TgMediaAccountService {
    @Autowired
    private SsoService ssoService;
    @Override
    public Page<TgMediaAccount> listTgMediaAccount(BaseFilter req) {

        Page<TgMediaAccount> pageTgMediaAccount = baseMapper.pageTgMediaAccount(req
                ,ssoService.isAdminToUserId(CurrentUserContext.getCurrentUser())
                , new Page<>(req.getPageNum() , req.getPageSize()) );
        return pageTgMediaAccount;
    }

    @Override
    public Boolean isExist(String accountNum) {
        Integer count = count(new LambdaQueryWrapper<TgMediaAccount>()
                .eq(TgMediaAccount::getAccountNum , accountNum)
                .eq(BasePojo::getIsDel , 0));
        if (count>0){
            return true ;
        }
        return false;
    }

    @Override
    public void addOrUpadteTgMediaAccount(TgMediaAccountAddOrUpdate res) {
        User user = CurrentUserContext.getCurrentUser();
        Boolean isExist =  isExist(res.getAccountNum());

        if (res.getId() != null){
            //更新数据
            TgMediaAccount bo = getById(res.getId());
            Assert.state(null != bo && null != bo.getId(), NULL_MSG);
            BeanUtil.copyProperties(res, bo);
            BasePojo.setBase(user.getId().longValue(), false, bo);
            updateById(bo);
        }else {
            if (isExist){
                throw new IllegalStateException("账户已存在");
            }
            TgMediaAccount tgMediaAccount = new TgMediaAccount(res , user);
            save(tgMediaAccount);
        }
    }

    @Override
    public void delTgMediaAccount(Long softnessId) {
        User user = CurrentUserContext.getCurrentUser();
        Integer count = selectCount(baseMapper, s -> s.eq(BasePojo::getId, softnessId));
        Assert.state(null != count && 0 < count, "未找到指定信息，请刷新重试");
        TgMediaAccount bo = new TgMediaAccount();
        bo.setId(softnessId);
        bo.setIsDel(1);
        BasePojo.setBase(user.getId().longValue(), false, bo);
        updateById(bo);
    }
}
